/*
重庆时时彩 1星
*/
cc.Class({
    extends: cc.Component,

    properties: {
        fbSelBall: cc.Prefab,//选择球
        betContent:cc.Node,//投注选择面板
        ndMiss:cc.Node,
        sscPop:cc.Node,//根节点

        rules:[],//规则
        nums:[],//数值
        _curTopTitle:"一星直选",//当前玩法名
        _lotteryID:0,//彩种id
        _curSelects:[],//选择球数字记录
        _isMiss:false,//是否开启遗漏
        _sscManage:null,
       
        //_betManage:null,
    },

     //重置初始值
    initReset:function(){
        //if(this._isMiss )
        //{
            //this._betManage.showMiss(this.betContent.children,this._lotteryID,this._lotteryID + this.rules.toString());
        //}
    },

    //外部传参
    init:function(lotteryid){
        this._lotteryID = lotteryid;  
    },

    showMiss:function(data){
        var childs = this.ndMiss.children;
        for(var i=0;i<childs.length;i++)
        {
            if(i<data[0].length)
            {
                childs[i].getComponent(cc.Label).string = data[0][i];
            }
        }
        this.ndMiss.active = true;
    },

    hideMiss:function(){
        this.ndMiss.active = false;
    },

    clearAllBetRecord:function(){
        this.clearCurSel();
    },

    // use this for initialization
    start: function () {
        this._sscManage = this.sscPop.getComponent(this.sscPop.name);
        if(this._sscManage == null)
        {
            ComponentsUtils.showTips("投注面板加载失败");
            return;
        }
        this._lotteryID = DEFINE.LOTTERY_ID.SSC_CQ;
        this.nums = ["0","1","2","3","4","5","6","7","8","9"]; 
        this.rules = DEFINE.LOTTERYRULESSC.ZHI_ONE;
        this.ndMiss.active = false;

        for(var j = 0;j<this.nums.length;j++)
        {      
            var ball = cc.instantiate(this.fbSelBall);
            ball.getComponent(ball.name).init(j);
            WidgetExtUtils.addCheckEvent(ball,this.node,this.node.name,"onClickCallback",j);
            this.betContent.addChild(ball);
        }
      // this._betManage = cc.find("Canvas").getChildByName("ChatRoomPage").getChildByName("BetManage").getComponent("BetManage");
    },

    onClickCallback:function(toggle, customEventData){
         if(toggle.getComponent(cc.Toggle).isChecked)
         { 
             this._curSelects.push(customEventData);
         }
         else
         {
             Utils.removeByValue(this._curSelects,customEventData);
         }  
        this.updateTgSelect();
        this.checkBet();
    },

    //检查倍数
    checkBet:function(){
        var bet = 0;
        bet = this._curSelects.length;
        this._sscManage.showAmount(bet);
    },

    //清空当前选择
    clearAllBetRecord:function(){
        this._curSelects.length = 0;
        for(var i = 0;i<this.betContent.children.length;i++)
        {
            this.betContent.children[i].getComponent(cc.Toggle).isChecked = false;
        }
    },

    //机选
    randomSelectCallBack:function(){
        this.clearAllBetRecord();
        var randomArray = Utils.getRandomArrayWithArray(this.betContent.children, 1);
        if(randomArray.length>0)
        {
            randomArray[0].getComponent(cc.Toggle).isChecked = true;
            var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
            this.onClickCallback(randomArray[0],temp);
            return true;
        }
        return false;
    },

    updateTgSelect:function(){
        if(this._curSelects.length > 0)
        {
            window.Notification.emit("updateTgSelect",false);
        }
        else
        {   
            window.Notification.emit("updateTgSelect",true);
        }
    },

    //投注信息组合
    dataObj:function(){
        //组合
        var obj = new Object(); 
        obj.PlayCode = parseInt(this._lotteryID + this.rules); 
        var nums = "";
        Utils.sortNum( this._curSelects);
        for(var i=0;i<this._curSelects.length;i++)
        {
            if(nums != "")
                nums += ",";
            nums += this._curSelects[i];
        }
        obj.num = nums;
        return obj;
    },

    onMiss:function(toggle){
        // if(toggle.getComponent(cc.Toggle).isChecked)
        // {
        //     this._isMiss = true;
        //     this._betManage.showMiss(this.betContent.children,this._lotteryID,this._lotteryID + this.rules.toString());
        // }    
        // else
        // {
        //     this._isMiss = false;
        //     this._betManage.setMiss(false,this.betContent.children,"");
        // }
    },
});
